package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.WidgetLanguageBinding
import com.polaris.live.utils.CommonUtils

/**
 * 语言控件
 *
 * @author Created by lucas on 2023/10/22 16:32
 * @since 1.0.0
 */
class LanguageView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetLanguageBinding.inflate(LayoutInflater.from(context), this)

    /**
     * 设置语言
     */
    fun setLanguage(languageCode: String?, width: Int = 12.autoDp, height: Int = 12.autoDp, textSp: Int = 11) {
        //适配以前的用户   有的用户之前因为没做限制导致以字符串的null传到了服务端
        val isNull = languageCode == "null" || languageCode == "NULL"
        if (languageCode != null && !isNull) {
            visibility = VISIBLE
            val lp = mBinding.laImg.layoutParams
            lp.width = width
            lp.height = height
            mBinding.laImg.layoutParams = lp
            mBinding.tvLanguage.textSize = textSp.toFloat()
            val languageName = CommonUtils.getLanguageNameFromLocaleCode(languageCode)
            if (languageName.isEmpty()) {
                visibility = GONE
            } else {
                mBinding.tvLanguage.text = CommonUtils.getLanguageNameFromLocaleCode(languageCode)
            }
        } else {
            visibility = GONE
        }
    }
}